/*
 * @lc app=leetcode.cn id=566 lang=typescript
 *
 * [566] 重塑矩阵
 */

// @lc code=start
function matrixReshape(mat: number[][], r: number, c: number): number[][] {
    if (r * c === mat.length * mat[0].length) {
        let newMat: number[][] = Array(r)
            .fill(0)
            .map((i) => Array(c).fill(0));
        mat.forEach((row, y) => {
            return row.forEach((it, x) => {
                const index = y * row.length + x;
                const w = index % c;
                const h = Math.floor(index / c);
                // console.log(y, x, "->", h, w);
                newMat[h][w] = it;
            });
        });
        return newMat;
    } else {
        return mat;
    }
}
// @lc code=end
